package com.hrt.datasender.datadao.service;

import java.util.ArrayList;
import java.util.List;

import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.db.wherefilter.WhereFilterType;
import com.btcode.orm.executer.IORMExecuter;
import com.btcode.orm.filter.FilterSingle;
import com.btcode.orm.filter.IFilter;
import com.btcode.orm.model.ModelPageResult;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.db.DBManager;

public final class BcDeviceService {

    private static volatile BcDeviceService instance;

    private BcDeviceService() {

    }

    public static BcDeviceService getInstance() {
        if (instance == null) {
            synchronized (BcDeviceService.class) {
                if (instance == null) {
                    instance = new BcDeviceService();
                }
            }
        }
        return instance;
    }

    public BcDevicesModel create(BcDevicesModel model) {
        IORMExecuter executer = DBManager.getORMExecuter();
        executer.create(model);
        return model;
    }

    public void update(BcDevicesModel model) {
        IORMExecuter executer = DBManager.getORMExecuter();
        executer.update(model);
    }

    public void delete(String id) {
        BcDevicesModel model = new BcDevicesModel();
        model.setId(id);
        BcDeviceMonitorParamService.getInstance().deleteByDevice(id);
        DBManager.getORMExecuter().delete(model);
    }

    public BcDevicesModel getModel(String id) {
        BcDevicesModel model = new BcDevicesModel();
        model.setId(id);
        model = DBManager.getORMExecuter().getModel(model);
        return model;
    }

    /**
     * 获取注册的设备
     * 
     * @return
     */
    public List<BcDevicesModel> getDevices() {
        ModelPageResult mpr = DBManager.getORMExecuter().getModelList(BcDevicesModel.class, null,null);
        List<BcDevicesModel> models = mpr.getData();
        return models;

    }

    /**
     * 获取仪器传递的参数
     * 
     * @param comId
     * @return
     */
    public List<BcDeviceMonitorParamModel> getMonitorParamByDevice(String comId) {

        List<IFilter> filters = new ArrayList<>();
        FilterSingle filter = new FilterSingle(BcDeviceMonitorParamModel.comNum, WhereFilterType.EQUAL, comId);
        filters.add(filter);
        ModelPageResult mpr = DBManager.getORMExecuter()
                .getModelList(BcDeviceMonitorParamModel.class, filters,null);
        List<BcDeviceMonitorParamModel> models = mpr.getData();
        return models;

    }

    /**
     * 获取站点ID和名称
     * 
     * @param id
     *            and deviceName
     * 
     */

    public BcDevicesModel getPlatformInfo(String id, String deviceName) {
        String sql = "SELECT * FROM BC_DEVICES WHERE ID='" + id + "' AND DEVICENAME='" + deviceName + "'";
        try {
            SQLParameterList pc = new SQLParameterList();
            List<BcDevicesModel> list = DBManager.getORMExecuter().getModelListSQL(sql, pc, BcDevicesModel.class);
            if (list.size() > 0){
                return list.get(0);
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}